using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CocaCola_Inventory_System.Forms
{
    public partial class BU : Form
    {
        DataTable data;
        retrieve r;
        Insert i;
        public BU()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            if (i.insertbu(textBox1.Text, textBox2.Text, ((CocaCola_Inventory_System.Forms.Container)this.MdiParent).id))
            {
                MessageBox.Show("BU Inserted Successfully");
                refresh_data();
            }
            else
                MessageBox.Show("Failed to insert BU");
                     
        }

        private void BU_Load(object sender, EventArgs e)
        {
            if (((CocaCola_Inventory_System.Forms.Container)this.MdiParent).security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            r = new retrieve(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con);
            i = new Insert(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con);
            refresh_data();
        }

        public void refresh_data()
        {
            if(!backgroundWorker1.IsBusy)
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            
            data = r.getBU();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            dataGridView1.DataSource = data;
            dataGridView1.Columns[0].Visible = false;
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedCells[0].RowIndex < 0 || dataGridView1.SelectedCells[0].RowIndex >= dataGridView1.Rows.Count)
                return;

            groupBox2.Visible = true;
            textBox3.Text = dataGridView1.SelectedCells[0].OwningRow.Cells[1].Value.ToString();
            textBox4.Text = dataGridView1.SelectedCells[0].OwningRow.Cells[2].Value.ToString();
            
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            groupBox2.Visible = false;
            textBox3.Text = "";
            textBox4.Text = "";
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            refresh_data();
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
                return;

            dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
                return;

            dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show("Deleting this BU will result in deleting all its item, Do you want to Procced ?? ", "Confirmation", MessageBoxButtons.YesNo);
            if (res != DialogResult.Yes)
                return;
            i.deletebu(Convert.ToInt32(dataGridView1.SelectedCells[0].OwningRow.Cells[0].Value), ((CocaCola_Inventory_System.Forms.Container)this.MdiParent).id);
            refresh_data();

            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (i.updatebu(Convert.ToInt32(dataGridView1.SelectedCells[0].OwningRow.Cells[0].Value), textBox3.Text, textBox4.Text, ((CocaCola_Inventory_System.Forms.Container)this.MdiParent).id))
            {
                MessageBox.Show("BU Updated Successfully");
                refresh_data();
            }
            else
                MessageBox.Show("Failed to update BU");

        }


    }
}